'use strict';

// Declare app level module which depends on views, and components
angular.module('myApp', [
  'ngRoute',
  'myApp.home',
  'myApp.sign',
  'myApp.calander',
  'myApp.members',
  'myApp.view1',
  'myApp.view2',
  'myApp.version'
])

.config(['$routeProvider', function ($routeProvider) {
    // $routeProvider.otherwise({redirectTo: '/view1'});
}])

.run(['$rootScope', function ($rootScope) {
    $rootScope.signinUser = {
        username: '',
        password: '',
        isSignin: false
    };

    $rootScope.showSignin = function () {
        $('#signin-popup').modal('open');
    };

    $rootScope.signin = function (callback) {
        var username = $rootScope.signinUser.username;
        var password = $rootScope.signinUser.password;
        var usernameInput = $('#signin-username');
        var passwordInput = $('#signin-password');
        var isValid = true;

        if (!username) {
            isValid = false;
            usernameInput.addClass('am-form-field');
            usernameInput.parent().addClass('am-form-error');
        } else {
            usernameInput.removeClass('am-form-field');
            usernameInput.parent().removeClass('am-form-error');
        }

        if (!password) {
            isValid = false;
            passwordInput.addClass('am-form-field');
            passwordInput.parent().addClass('am-form-error');
        } else {
            passwordInput.removeClass('am-form-field');
            passwordInput.parent().removeClass('am-form-error');
        }

        if (!isValid) {
            return;
        }

        if (username == 'iburning' && password == '111') {
            $rootScope.signinUser.isSignin = true;
            $('#signin-popup').modal('close');
            if ('function' == typeof callback) {
                callback();
            }
        } else {
            usernameInput.addClass('am-form-field');
            usernameInput.parent().addClass('am-form-error');
            passwordInput.addClass('am-form-field');
            passwordInput.parent().addClass('am-form-error');
            $rootScope.signinUser = {
                username: '',
                password: '',
                isSignin: false
            };
        }
    };

    $rootScope.signout = function () {
        $rootScope.signinUser = {
            username: '',
            password: '',
            isSignin: false
        };
    };

    var weiboOauth = function () {
        openWindow('/weibo/oauth');
    };
}]);
